import { request } from '@/service/request';

/**
 * 数据开发任务类型
 */
export interface DevTask {
  id?: number;
  name: string;
  taskType: string;
  content: string;
  datasourceId?: number;
  datasourceName?: string;
  engineType?: string;
  engineConfig?: string;
  scheduleType?: string;
  scheduleConfig?: string;
  dsWorkflowId?: number;
  status?: number;
  projectId?: number;
  description?: string;
  createTime?: string;
  updateTime?: string;
}

/**
 * 任务实例类型
 */
export interface DevInstance {
  id: number;
  taskId: number;
  taskName?: string;
  dsInstanceId?: number;
  status: string;
  startTime?: string;
  endTime?: string;
  duration?: number;
  logs?: string;
  errorMsg?: string;
  resultRows?: number;
  createTime?: string;
}

/**
 * 执行计划类型
 */
export interface ExecutionPlan {
  planText: string;
  steps: ExecutionStep[];
  estimatedCost?: number;
  estimatedRows?: number;
}

export interface ExecutionStep {
  stepId: number;
  operationType: string;
  tableName?: string;
  indexUsed?: string;
  filterCondition?: string;
  estimatedRows?: number;
  cost?: number;
}

/**
 * 查询数据开发任务列表
 */
export function fetchDevTaskList(params?: any) {
  return request<ApiDataplatform.PageResult<DevTask>>({
    url: '/dataplatform/dev/tasks',
    method: 'get',
    params
  });
}

/**
 * 查询数据开发任务详情
 */
export function fetchDevTaskDetail(id: number) {
  return request<DevTask>({
    url: `/dataplatform/dev/tasks/${id}`,
    method: 'get'
  });
}

/**
 * 创建数据开发任务
 */
export function addDevTask(data: DevTask) {
  return request<number>({
    url: '/dataplatform/dev/tasks',
    method: 'post',
    data
  });
}

/**
 * 更新数据开发任务
 */
export function updateDevTask(id: number, data: DevTask) {
  return request({
    url: `/dataplatform/dev/tasks/${id}`,
    method: 'put',
    data
  });
}

/**
 * 删除数据开发任务
 */
export function deleteDevTask(id: number) {
  return request({
    url: `/dataplatform/dev/tasks/${id}`,
    method: 'delete'
  });
}

/**
 * 执行SQL任务
 */
export function executeSqlTask(id: number, params?: any) {
  return request<any>({
    url: `/dataplatform/dev/tasks/${id}/execute-sql`,
    method: 'post',
    data: params || {}
  });
}

/**
 * 执行脚本任务
 */
export function executeScriptTask(id: number, params?: any) {
  return request<any>({
    url: `/dataplatform/dev/tasks/${id}/execute-script`,
    method: 'post',
    data: params || {}
  });
}

/**
 * 停止任务执行
 */
export function stopExecution(instanceId: number) {
  return request({
    url: `/dataplatform/dev/instances/${instanceId}/stop`,
    method: 'post'
  });
}

/**
 * 获取执行计划
 */
export function fetchExecutionPlan(id: number) {
  return request<ExecutionPlan>({
    url: `/dataplatform/dev/tasks/${id}/execution-plan`,
    method: 'get'
  });
}

/**
 * 查询任务实例列表
 */
export function fetchDevInstanceList(taskId: number, params?: any) {
  return request<ApiDataplatform.PageResult<DevInstance>>({
    url: `/dataplatform/dev/tasks/${taskId}/instances`,
    method: 'get',
    params
  });
}

/**
 * 查询实例详情
 */
export function fetchDevInstanceDetail(instanceId: number) {
  return request<DevInstance>({
    url: `/dataplatform/dev/instances/${instanceId}`,
    method: 'get'
  });
}

/**
 * 发布任务
 */
export function publishDevTask(id: number) {
  return request({
    url: `/dataplatform/dev/tasks/${id}/publish`,
    method: 'post'
  });
}

/**
 * 下线任务
 */
export function offlineDevTask(id: number) {
  return request({
    url: `/dataplatform/dev/tasks/${id}/offline`,
    method: 'post'
  });
}
